﻿using System;
using Wise.TimeWarrior.DTO;
using Wise.TimeWarrior.Forms;
using Wise.TimeWarrior.Interfaces;
using Wise.TimeWarrior.OSFunctionalitiesProxy;

namespace Wise.TimeWarrior.Business.Impl
{
    /// <summary>
    /// <see cref="IWorkingDaysStatisicsBusinessLogic"/>
    /// </summary>
    public class WorkingDaysStatisicsBusinessLogic : IWorkingDaysStatisicsBusinessLogic
    {
        /// <summary>
        /// <see cref="IExportData"/>
        /// </summary>
        public IExprotData ExprotDataService { set; private get; }

        private StatistcsForm statistcsForm = null;
        /// <summary>
        /// <see cref="IWorkingDaysStatisicsBusinessLogic.DataStoreService"/>
        /// </summary>
        public IDataStore DataStoreService { set; get; }
        /// <summary>
        /// <see cref="IWorkingDaysStatisicsBusinessLogic.WorkingDayKeeper"/>
        /// </summary>
        public IWorkingDayKeeper WorkingDayKeeper { set; private get; }
        /// <summary>
        /// <see cref="IWorkingDaysStatisicsBusinessLogic.Configuration"/>
        /// </summary>
        public IConfiguration Configuration { set; private get; }
        /// <summary>
        /// <see cref="IWorkingDaysStatisicsBusinessLogic.DateTimeProvider"/>
        /// </summary>
        public IDateTimeProvider DateTimeProvider { set; private get; }
        /// <summary>
        /// <see cref="IWorkingDaysStatisicsBusinessLogic.GetStatistics"/>
        /// </summary>
        public BusinessItemDto GetStatistics()
        {
            BusinessItemDto itemDto = new BusinessItemDto()
            {
                Action = () =>
                             {
                                 statistcsForm.WorkingDayList =
                                     WorkingDayKeeper.GetDataInformationForSpecifiedDates(
                                         new DateTime(DateTimeProvider.Now().Year, DateTimeProvider.Now().Month, 1));
                    statistcsForm.Show();
                },
                ActionOption = BusinessItemOption.Visible,
                OriginalMethodName = "GetStatistics"
            };
            return itemDto;
        }
        /// <summary>
        /// <see cref="IDisposable.Dispose"/>
        /// </summary>
        public void Dispose()
        {
            statistcsForm.Dispose();
        }
        /// <summary>
        /// <see cref="IBLSetup.SetupLogic"/>
        /// </summary>
        public void SetupLogic()
        {
            statistcsForm = new StatistcsForm();
            statistcsForm.DateChanged += statistcsForm_DateChanged;
            statistcsForm.DoSnapshoot += DoSnapshoot;
        }

        private void DoSnapshoot(object sender, EventArgs e)
        {
            WorkingDayKeeper.AddImmediate();
        }

        /// <summary>
        /// logic for operation on date filter on form
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        void statistcsForm_DateChanged(DateTime from, DateTime? to)
        {
            statistcsForm.WorkingDayList =
                   WorkingDayKeeper.GetDataInformationForSpecifiedDates(
                       new DateTime(from.Year, from.Month, from.Day),
                       to.HasValue ? new DateTime(to.Value.Year, to.Value.Month, to.Value.Day) : (DateTime?)null );
        }




        public BusinessItemDto ExportStatistics()
        {
            BusinessItemDto itemDto = new BusinessItemDto()
            {
                Action = () =>
                {
                    ExprotDataService.ExportWorkingDayData(statistcsForm.WorkingDayList);
                },
                ActionOption = BusinessItemOption.Visible,
                OriginalMethodName = "ExportStatistics"
            };
            return itemDto;
        }
    }

}
